
importPackage(Packages.client);
importPackage(Packages.client.inventory);
var 红色箭头 = "#fUI/UIWindow/Quest/icon6/7#";
var opt = 0;

var status = 0;

function start() {
	status = -1;
	action(1, 0, 0);
}

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (mode == 0 && status == 0) {
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
            status--;
            
        var mapleLeaf = 4001126;
        var oneMillion = 1000000;
        var oneMillionDesc = "100万"
        var five = 5
            
		if (status == 0) {
            var text = "";
            
            text+="#L1#" + 红色箭头 + "#r" + oneMillionDesc + "金币 #k= #b1人气点\r\n\r\n";
            text+="#L2#" + 红色箭头 + "#r" + five + "个#v" + mapleLeaf +"##z" + mapleLeaf + "# #k= #b1人气点\r\n\r\n";
            
			cm.sendSimple(text);
        } else if (status == 1) {
            if (selection == 1 && status != 2) {
                opt = 1
            } else if (selection == 2 && status != 2) {
                opt = 2
            }
            cm.sendGetNumber("请输入您要兑换的#b人气点数#k（一次最多兑换99）", 1, 1, 99);
        } else if (status == 2) {
            // 金币兑换
            if(opt == 1) {
                var needCoin = oneMillion * selection
                if(cm.getMeso() >= needCoin) {
                    cm.gainMeso(-needCoin)
                    cm.getPlayer().addFame(selection)
                    cm.sendOk("兑换成功！游戏重登录后刷新！")
                } else{
                    cm.sendOk("你没有足够的金币！")
                }
            } else if (opt == 2) {
                var needQutity = five * selection
                if(cm.haveItem(mapleLeaf, needQutity)){
					cm.gainItem(mapleLeaf, -needQutity);
                    cm.getPlayer().addFame(selection)
                    cm.sendOk("兑换成功！游戏重登录后刷新！")
                } else {
                    cm.sendOk("你没有足够的#r#v" + mapleLeaf +"##z" + mapleLeaf + "#！")
                }
            }

            cm.dispose();
        }
    }
}